<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="标题："  prop="title" required>
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容：" prop="content" required>
        <el-input type="textarea" rows="13"  v-model="form.content"></el-input>
      </el-form-item>
      <el-form-item label="选择测试：" required prop="taskId">
        <el-select v-model="form.taskId"
          placeholder="选择测试"
          :loading="selectLoading">
          <el-option v-for="item in taskList" :key="item.id" :label="item.title" :value="item.id" @click.native="updateResut(item.id)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="结果类型：" required prop="resultId">
        <el-select v-model="form.resultId" multiple
          placeholder="选择结果类型"
          :loading="selectLoading">
          <el-option v-for="item in testResult" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">发送</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import userApi from '@/api/user'
import messageApi from '@/api/message'
import taskApi from '@/api/dashboard'

export default {
  data () {
    return {
      form: {
        title: '',
        content: '',
        taskId: '',
        resultId: []
      },
      taskList: [],
      testResult: [ { id: '', name: '' } ],
      formLoading: false,
      selectLoading: false,
      options: [],
      rules: {
        title: [
          { required: true, message: '请输入消息标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入消息内容', trigger: 'blur' }
        ],
        taskId: [
          { required: true, message: '请选择测试', trigger: 'change' }
        ],
        resultId: [
          { required: true, message: '请选择结果类型', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    this.initTaskList()
  },
  methods: {
    initTaskList () {
      taskApi.batchTaskList().then(data => {
        this.taskList = data.response
      })
    },
    updateResut (taskId) {
      taskApi.getTestResultByTaskId(taskId).then(data => {
        this.testResult = data.response
        this.form.resultId = []
        this.selectedArr = []
      })
    },
    getUserByUserName (query) {
      let _this = this
      if (query !== '') {
        _this.selectLoading = true
        userApi.getTaskList(query).then(re => {
          _this.selectLoading = false
          _this.options = re.response
        })
      } else {
        _this.options = []
      }
    },
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          messageApi.send(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.delCurrentView(_this).then(() => {
                _this.$router.push('/dashboard')
              })
            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        } else {
          return false
        }
      })
    },
    resetForm () {
      let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        title: '',
        content: '',
        receiveUserIds: []
      }
      this.form.id = lastId
    },
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  }
}
</script>
